﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.productionworkorder;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SCWorkOrderCommand;
using T.E.ERP.Write.API.Applicantions.Command.ZJWorkOrderCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ZJWorkOrderCommandHandler
{
    /// <summary>
    /// 质检工单基本信息增加命令处理方法
    /// </summary>
    public class ZJWorkOrderAddCommandHandler : IRequestHandler<ZJWorkOrderAddCommand, APIResult<QualityInspectionWorkOrder>>
    {
        /// <summary>
        /// 质检工单的仓储借口
        /// </summary>
        private readonly IBaseRepository<QualityInspectionWorkOrder> qRepo;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="qRepo"></param>
        public ZJWorkOrderAddCommandHandler(IBaseRepository<QualityInspectionWorkOrder>qRepo,IMapper mapper)
        {
            this.qRepo = qRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<QualityInspectionWorkOrder>> Handle(ZJWorkOrderAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<QualityInspectionWorkOrder> result = new APIResult<QualityInspectionWorkOrder>();
           
            var r = mapper.Map<QualityInspectionWorkOrder>(request);
            r.Status = false;
            r.IsDel = false;
            int s = await qRepo.Add(r);
            if(s>0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = r;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = r;
            }
            return await Task.FromResult(result);

        }


    }

}
